#include <bits/stdc++.h>
// 2025/04/05
// tag:
// Author: Forever
using namespace std;
using int64 = int64_t;

class edge {
public:
    int u, v, w;
    edge(int u, int v, int w)
        : u(u)
        , v(v)
        , w(w) { };
    edge()
        : u(0)
        , v(0)
        , w(0) { };
    bool operator<(const edge& e) const
    {
        return w > e.w;
    }
};

int n, m, q, ans[30001];
edge edges[50001];

class UF {
public:
    int parents[10001], cnt;
    unordered_set<int> Q[10001];
    UF(int n)
    {
        cnt = n;
        for (int i = 1; i <= n; i++)
            parents[i] = i;
    };

    int find(int i)
    {
        if (parents[i] != i)
            return parents[i] = find(parents[i]);
        return parents[i];
    }

    void un(int i, int j, int x)
    {
        int ri = find(i), rj = find(j);
        if (ri == rj)
            return;
        if (Q[ri].size() > Q[rj].size())
            swap(ri, rj);
        vector<int> v;
        for (int it : Q[ri]) {
            if (Q[rj].count(it)) {
                ans[it] = x;
                v.push_back(it);
            }
            Q[rj].insert(it);
        }
        for (int it : v)
            Q[ri].erase(it);
        parents[ri] = rj;
        cnt--;
    }

    int count()
    {
        return cnt;
    }

    bool connected(int p, int q)
    {
        return find(p) == find(q);
    }
};

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        edges[i] = edge(u, v, w);
    }
    sort(edges + 1, edges + m + 1);
    UF uf(n);
    cin >> q;
    for (int i = 1; i <= q; i++) {
        int x, y;
        cin >> x >> y;
        uf.Q[x].insert(i);
        uf.Q[y].insert(i);
        ans[i] = -1;
    }
    for (int i = 1; i <= m; i++) {
        if (uf.connected(edges[i].u, edges[i].v))
            continue;
        uf.un(edges[i].u, edges[i].v, edges[i].w);
    }
    for (int i = 1; i <= q; i++)
        cout << ans[i] << "\n";
    return 0;
}